#include<iostream>

using namespace std;

int fibonacci(int n, int a, int b) {
	if(n == 1 || n == 2) {
		return 1;
	}
	return a * fibonacci(n - 1, a, b) + b * fibonacci(n - 2, a, b);
}

int main(void) {
	int n, a, b, p;
	cin >> n >> a >> b >> p;
	int result =  fibonacci(n, a, b) % p;
	cout << result << endl; 
	return 0;
}
